/**
 * @file    app_lumbar.c
 * @author  yhy
 * @brief   腰部系统应用层
 * @version 1.00
 * @date    2025-07-30
 *
 * @copyright Copyright (c) 2025
 *
 */
#include "app_lumbar.h"

#include "inf_pump.h"

static void     app_lumbar_apply_mode(airbag_system_t *system, uint32_t mode);
static result_e app_lumbar_validate_mode(uint32_t mode);

static airbag_system_t lumbar_system;
static airbag_t        lumbar_airbags[LUMBAR_AIRBAG_IDX_MAX];
static cps121_idx_e    lumbar_sensor_mapping[LUMBAR_AIRBAG_IDX_MAX];

/* 阀门通道配置 */
static airbag_valve_config_t lumbar_valve_configs[LUMBAR_AIRBAG_IDX_MAX] = {
    [LUMBAR_AIRBAG_IDX_UP] = {
        .inflate_valve_ch = SOLENOID_VALVE_CH26, // YT9
        .deflate_valve_ch = SOLENOID_VALVE_CH27, // YTR9
        .pressure_sensor_idx = CPS121_IDX_2,
    },
    [LUMBAR_AIRBAG_IDX_DOWN] = {
        .inflate_valve_ch = SOLENOID_VALVE_CH24, // YT8
        .deflate_valve_ch = SOLENOID_VALVE_CH25, // YTR8
        .pressure_sensor_idx = CPS121_IDX_1,
    },
};

/* 腰部系统配置 */
static const airbag_system_config_t lumbar_config = {
    .max_inflate_time_ms    = 10 * 1000, // TODO 待修改为10s
    .dispatching_cycle_ms   = 50,
    .airbag_count           = LUMBAR_AIRBAG_IDX_MAX,
    .valve_configs          = lumbar_valve_configs,
    .air_pressure_threshold = 165, /* 气压保护阈值 165 KPa */
};

/* 回调函数集合 */
static const airbag_system_callbacks_t lumbar_callbacks = {
    .apply_mode    = app_lumbar_apply_mode,
    .validate_mode = app_lumbar_validate_mode,
};

/**
 * @brief 初始化腰部系统
 *
 * @return result_e 初始化结果, 初始化成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e app_lumbar_init(void)
{
    lumbar_system.airbags        = lumbar_airbags;
    lumbar_system.sensor_mapping = lumbar_sensor_mapping;
    return app_airbag_system_init(&lumbar_system, &lumbar_config, AIRBAG_SYSTEM_TYPE_LUMBAR,
                                  &lumbar_callbacks);
}

/**
 * @brief 设置腰部系统运行模式
 *
 * @param   mode        运行模式
 * @return  result_e    设置结果, 设置成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e app_lumbar_set_mode(lumbar_mode_e mode)
{
    return app_airbag_system_set_mode(&lumbar_system, (uint32_t)mode);
}

/**
 * @brief 停止腰部系统运行
 *
 * @return result_e 停止结果, 停止成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
result_e app_lumbar_stop(void) { return app_airbag_system_stop(&lumbar_system); }

/**
 * @brief 处理腰部系统运行状态, 在调度器中循环调度
 *
 */
void app_lumbar_handle(void) { app_airbag_system_handle(&lumbar_system); }

/**
 * @brief 获取当前腰部系统运行模式
 *
 * @return lumbar_mode_e 当前运行模式
 */
lumbar_mode_e app_lumbar_get_mode(void)
{
    return (lumbar_mode_e)app_airbag_system_get_mode(&lumbar_system);
}

/**
 * @brief 获取指定腰部系统气袋(支撑)状态
 *
 * @param   airbag_idx      气袋索引
 * @return  airbag_state_e  气袋支撑状态
 */
airbag_state_e app_lumbar_get_airbag_state(lumbar_airbag_idx_e airbag_idx)
{
    return app_airbag_system_get_airbag_state(&lumbar_system, (uint32_t)airbag_idx);
}

/**
 * @brief 获取指定腰部系统气袋压力值
 *
 * @param airbag_idx    气袋索引
 * @return uint32_t     气袋压力值, 单位为KPa, 0表示未获取到
 */
uint32_t app_lumbar_get_air_pressure(lumbar_airbag_idx_e airbag_idx)
{
    return app_airbag_system_get_air_pressure(&lumbar_system, (uint32_t)airbag_idx);
}

/**
 * @brief 应用腰部系统运行模式
 *
 * @param system    腰部系统指针
 * @param mode      运行模式
 */
static void app_lumbar_apply_mode(airbag_system_t *system, uint32_t mode)
{
    lumbar_mode_e lumbar_mode = (lumbar_mode_e)mode;

    switch (lumbar_mode) {
    case LUMBAR_MODE_FRONT: // front 同充
        // inf_pump_air_inflation();
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_UP, AIRBAG_STATE_INFLATE);
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_DOWN, AIRBAG_STATE_INFLATE);
        break;
    case LUMBAR_MODE_REAR: // rear 同泄，抽气
        // inf_pump_air_exhaust();
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_UP, AIRBAG_STATE_EXHAUST);
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_DOWN, AIRBAG_STATE_EXHAUST);
        break;
    case LUMBAR_MODE_F_R_KEEP: // f r 同保压
        // inf_pump_air_stop();
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_UP, AIRBAG_STATE_KEEP);
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_DOWN, AIRBAG_STATE_KEEP);
        break;
    case LUMBAR_MODE_UP: // up 上充下泄
        // inf_pump_air_inflation();
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_UP, AIRBAG_STATE_INFLATE);
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_DOWN, AIRBAG_STATE_DEFLATE);
        break;
    case LUMBAR_MODE_DOWN: // down 下充上泄
        // inf_pump_air_inflation();
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_UP, AIRBAG_STATE_DEFLATE);
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_DOWN, AIRBAG_STATE_INFLATE);
        break;
    case LUMBAR_MODE_U_D_KEEP: // u d 同保压
        // inf_pump_air_stop();
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_UP, AIRBAG_STATE_KEEP);
        app_airbag_system_ctrl_airbag(system, LUMBAR_AIRBAG_IDX_DOWN, AIRBAG_STATE_KEEP);
        break;
    }
}

/**
 * @brief 验证腰部系统运行模式
 *
 * @param   mode         运行模式
 * @return  result_e     验证结果, 验证成功返回RESULT_STATUS_OK, 失败返回其他错误码
 */
static result_e app_lumbar_validate_mode(uint32_t mode)
{
    lumbar_mode_e lumbar_mode = (lumbar_mode_e)mode;
    if (lumbar_mode < LUMBAR_MODE_FRONT || lumbar_mode >= LUMBAR_MODE_MAX) {
        return RESULT_STATUS_INVALID_PARAM;
    }

    return RESULT_STATUS_OK;
}
